﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace Acoris.Core
{

    using Microsoft.Graphics.Canvas;
    using Microsoft.Graphics.Canvas.Brushes;
    using Microsoft.Graphics.Canvas.UI;
    using Microsoft.Graphics.Canvas.UI.Xaml;

    using Windows.System;
    using Windows.UI.Text;
    using Windows.UI.Text.Core;

    public sealed class AcorisTextEditor : ContentControl
    {

        #region Fields

        CoreTextServicesManager m_manager;
        CoreTextEditContext m_context;
        bool m_focusState;

        #endregion

        #region Constructor

        public AcorisTextEditor()
        {
            this.DefaultStyleKey = typeof(AcorisTextEditor);

            var canvas = new CanvasControl();
            canvas.Draw += OnCanvasDraw;
            canvas.CreateResources += OnCreateResources;
            this.Content = canvas;

            m_manager = CoreTextServicesManager.GetForCurrentView();
            m_context = m_manager.CreateEditContext();
            m_context.CompositionCompleted += OnCompositionCompleted;
            m_context.CompositionStarted += OnCompositionStarted;
            m_context.FocusRemoved += OnFocusRemoved;
            m_context.FormatUpdating += OnFormatUpdating;
            m_context.LayoutRequested += OnLayoutRequested;
            m_context.NotifyFocusLeaveCompleted += OnNotifyFocusLeaveCompleted;
            m_context.SelectionRequested += OnSelectionRequested;
            m_context.SelectionUpdating += OnSelectionUpdating;
            m_context.TextRequested += OnTextRequested;
            m_context.TextUpdating += OnTextUpdating;
            m_focusState = false;
        }

        #endregion

        #region TextService

        /// <summary>
        /// 文本输入服务需要修改文本输入控件里面的文本时发生.该事件可能是一个键盘事件的结果比如插入单个字符，或者是输入处理器处理结束的结果,比如 自动校正或预测.
        /// 当处理这个事件时文本输入控件将文本输入服务中提供的新文本替换到指定区域并把光标移动到新字符串后面.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnTextUpdating(CoreTextEditContext sender, CoreTextTextUpdatingEventArgs args)
        {
            var range = args.Range;
            var inputLanguage = args.InputLanguage;
            var newSelection =  args.NewSelection;
            var result = args.Result;
            var text = args.Text;            
        }

        /// <summary>
        /// 当文本输入服务需要在文本输入控件中获得一个文本范围时发生。应用需要处理这个时间，并返回范围请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnTextRequested(CoreTextEditContext sender, CoreTextTextRequestedEventArgs args)
        {
            var request = args.Request;
        }

        /// <summary>
        /// 当文本输入服务需要修改当前文本输入控件中的文本的选择范围时发生.该事件可能是
        /// could be the result of an input processor needing to select some text, or to move the caret. 
        /// The text input control should set its selection range accordingly.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnSelectionUpdating(CoreTextEditContext sender, CoreTextSelectionUpdatingEventArgs args)
        {
            var selection = args.Selection;
            var result = args.Result;            
        }

        /// <summary>
        /// Occurs when the text input server needs to get the text range representing the currently selected text in the text input control.
        /// The application should handle this event and return the range requested.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnSelectionRequested(CoreTextEditContext sender, CoreTextSelectionRequestedEventArgs args)
        {
            var request = args.Request;
        }

        /// <summary>
        /// 当焦点离开文本输入控件时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnNotifyFocusLeaveCompleted(CoreTextEditContext sender, object args)
        {
            
        }

        /// <summary>
        /// Occurs when the text input server needs to get the bounding box of a range of text and of the text input control itself.
        /// The application should handle this event and return the geometry information requested.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnLayoutRequested(CoreTextEditContext sender, CoreTextLayoutRequestedEventArgs args)
        {
            var request = args.Request;
        }

        /// <summary>
        /// Occurs when the text input server needs to apply a different format to a particular range of text. 
        /// This usually happens during composition.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnFormatUpdating(CoreTextEditContext sender, CoreTextFormatUpdatingEventArgs args)
        {
            var result = args.Result;
            var color = args.TextColor;
            var range = args.Range;
            var reason = args.Reason;
            var underlineColor = args.UnderlineColor;
            var underlineType =  args.UnderlineType;            
        }

        /// <summary>
        /// 当焦点从文本输入控件中被强制移除时发生.
        /// 应用程序应该捕获该事件以移除文本输入控件的焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnFocusRemoved(CoreTextEditContext sender, object args)
        {
            RemoveFocus();
        }

        /// <summary>
        /// 当组合操作开始时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnCompositionStarted(CoreTextEditContext sender, CoreTextCompositionStartedEventArgs args)
        {
            
        }

        /// <summary>
        /// 当组合操作完成时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnCompositionCompleted(CoreTextEditContext sender, CoreTextCompositionCompletedEventArgs args)
        {
            
        }

        #endregion

        #region Focus

        internal void SetFocus()
        {
            
        }

        internal void RemoveFocus()
        {

        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            SetFocus();
            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(RoutedEventArgs e)
        {
            RemoveFocus();
            base.OnLostFocus(e);
        }

        #endregion

        #region KeyboardEventHandler

        protected override void OnKeyDown(KeyRoutedEventArgs e)
        {
            switch (e.Key) {
                case VirtualKey.Left:
                    break;
                case VirtualKey.Right:
                    break;
                case VirtualKey.Up:
                    break;
                case VirtualKey.Down:
                    break;
            }
            base.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyRoutedEventArgs e)
        {
            base.OnKeyUp(e);
        }

        #endregion

        #region MouseEventHandler

        protected override void OnPointerCanceled(PointerRoutedEventArgs e)
        {
            base.OnPointerCanceled(e);
        }

        protected override void OnPointerCaptureLost(PointerRoutedEventArgs e)
        {
            base.OnPointerCaptureLost(e);
        }

        protected override void OnPointerEntered(PointerRoutedEventArgs e)
        {
            base.OnPointerEntered(e);
        }

        protected override void OnPointerExited(PointerRoutedEventArgs e)
        {
            base.OnPointerExited(e);
        }

        protected override void OnPointerMoved(PointerRoutedEventArgs e)
        {
            base.OnPointerMoved(e);
        }

        protected override void OnPointerPressed(PointerRoutedEventArgs e)
        {
            base.OnPointerPressed(e);
        }

        protected override void OnPointerReleased(PointerRoutedEventArgs e)
        {
            base.OnPointerReleased(e);
        }

        protected override void OnPointerWheelChanged(PointerRoutedEventArgs e)
        {
            base.OnPointerWheelChanged(e);
        }

        #endregion

        #region Method


        #endregion

        #region Property


        /// <summary>
        /// 获取当前文本的字符串值
        /// </summary>
        public string Text {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        /// <summary>
        /// Text属性的依赖属性
        /// </summary>
        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(AcorisTextEditor), new PropertyMetadata(null));



        #endregion

        #region Event

        #endregion

        #region PickupElement

        //---------------------------------------------------------------------------------------------------
        //  当前方法主要编写屏幕拾取相关的方法
        //---------------------------------------------------------------------------------------------------

        private void PickupElement()
        {

        }

        #endregion

        #region Drawing And CreateResource

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnCreateResources(CanvasControl sender, CanvasCreateResourcesEventArgs args)
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnCanvasDraw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            
        }

        #endregion
    }
}
